package com.ohos.carbon.slice.animations;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.utils.RandomImageUtil;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;


public class ImageFadeSlice extends AbilitySlice {

    private Image back_button, image1, image2;
    private Image fadeImage1, fadeImage2, fadeImage3, fadeImage4, fadeImage5, fadeImage6, fadeImage7, fadeImage8, fadeImage9;
    private Text text_title;
    private Button button_fade;
    private RandomImageUtil randomImageUtil;
    private boolean isShowOrNot = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_image_fade);
        //创建RandomImageUtil对象
        randomImageUtil = new RandomImageUtil(getContext());
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Image fade")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Image fade")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Image fade", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Image fade", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Image fade", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        //titlebar
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Image fade");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        //Image初始化
        button_fade = (Button) findComponentById(ResourceTable.Id_button_fade);
        fadeImage1 = (Image) findComponentById(ResourceTable.Id_fade_image1);
        fadeImage2 = (Image) findComponentById(ResourceTable.Id_fade_image2);
        fadeImage3 = (Image) findComponentById(ResourceTable.Id_fade_image3);
        fadeImage4 = (Image) findComponentById(ResourceTable.Id_fade_image4);
        fadeImage5 = (Image) findComponentById(ResourceTable.Id_fade_image5);
        fadeImage6 = (Image) findComponentById(ResourceTable.Id_fade_image6);
        fadeImage7 = (Image) findComponentById(ResourceTable.Id_fade_image7);
        fadeImage8 = (Image) findComponentById(ResourceTable.Id_fade_image8);
        fadeImage9 = (Image) findComponentById(ResourceTable.Id_fade_image9);
        //设置随机图片
        //第一张图片
        fadeImage1.setPixelMap(randomImageUtil.next2());
        //第二张图片
        fadeImage2.setPixelMap(randomImageUtil.next2());
        //第三张图片
        fadeImage3.setPixelMap(randomImageUtil.next2());
        //第四张图片
        fadeImage4.setPixelMap(randomImageUtil.next2());
        //第五张图片
        fadeImage5.setPixelMap(randomImageUtil.next2());
        //第六张图片
        fadeImage6.setPixelMap(randomImageUtil.next2());
        //第七张图片
        fadeImage7.setPixelMap(randomImageUtil.next2());
        //第八张图片
        fadeImage8.setPixelMap(randomImageUtil.next2());
        //第九张图片
        fadeImage9.setPixelMap(randomImageUtil.next2());
        //按钮点击事件
        button_fade.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                AnimatorProperty animator1 = fadeImage1.createAnimatorProperty();
                AnimatorProperty animator2 = fadeImage2.createAnimatorProperty();
                AnimatorProperty animator3 = fadeImage3.createAnimatorProperty();
                AnimatorProperty animator4 = fadeImage4.createAnimatorProperty();
                AnimatorProperty animator5 = fadeImage5.createAnimatorProperty();
                AnimatorProperty animator6 = fadeImage6.createAnimatorProperty();
                AnimatorProperty animator7 = fadeImage7.createAnimatorProperty();
                AnimatorProperty animator8 = fadeImage8.createAnimatorProperty();
                AnimatorProperty animator9 = fadeImage9.createAnimatorProperty();
                if (isShowOrNot == false) {
                    animator1.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(300);
                    animator2.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(500);
                    animator3.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(700);
                    animator4.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(700);
                    animator5.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(900);
                    animator6.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(1100);
                    animator7.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(1100);
                    animator8.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(1200);
                    animator9.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(1200);
                    isShowOrNot = true;
                } else {
                    animator1.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(100);
                    animator2.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(500);
                    animator3.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(900);
                    animator4.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(900);
                    animator5.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(1500);
                    animator6.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(2000);
                    animator7.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(2000);
                    animator8.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(2300);
                    animator9.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(2300);
                    isShowOrNot = false;
                }
                animator1.start();
                animator2.start();
                animator3.start();
                animator4.start();
                animator5.start();
                animator6.start();
                animator7.start();
                animator8.start();
                animator9.start();
            }
        });
        //点击返回上个页面
        back_button.setClickedListener(component -> {
            onBackPressed();
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
